package chapter7.No7_2_线程组.b线程对象关联线程组_多级关联;

/**
 * !在main组中添加一个线程组A，然后在这个线程组A中天津爱线程对象Z
 *
 * @author liujian
 * @version 2016/12/8 0008
 */
public class Main {

    static class MyThread implements Runnable {
        public void run() {
            System.out.println("进入RUN");
        }
    }

    public static void main(String[] args) {
        // 获取main线程的线程组
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();

        ThreadGroup aGroup = new ThreadGroup(mainGroup, "A");
        MyThread myThread = new MyThread();
        Thread z = new Thread(aGroup, myThread);
        z.setName("Z");
        z.start();
        Thread z2 = new Thread(aGroup, myThread);
        z2.setName("Z2");
        z2.start();

        System.out.println("================ 分割线 ================");
        // 查看main线程组中有多少个线程组,以及线程组的名字
        ThreadGroup[] paGroup = new ThreadGroup[mainGroup.activeGroupCount()];
        mainGroup.enumerate(paGroup);
        System.out.println("main线程组中线程组的个数：" + mainGroup.activeGroupCount());
        for (ThreadGroup ag : paGroup) {
            System.out.println("main线程组中线程组的名字：" + ag.getName());
        }

        System.out.println("================ 分割线 ================");
        // 查看线程组A中的线程
        Thread[] pzThread = new Thread[paGroup[0].activeCount()];
        paGroup[0].enumerate(pzThread);
        System.out.println("线程组A中线程的个数：" + paGroup[0].activeCount());
        for (Thread zt : pzThread) {
            System.out.println("线程组A中线程名字：" + zt.getName());
        }
    }
}
